//
//  XLHTTPRequest.m
//  xinLing
//
//  Created by 于鹏 on 2017/5/17.
//  Copyright © 2017年 于鹏. All rights reserved.
//

#import "XLHTTPRequest.h"
#import "PPNetworkHelper.h"
#import "XLRequestURLInfo.h"
#import "XLUserInfo.h"
#import <BaseModule/FunProgressHUD.h>

//NSString * const XLBaseURL = BaseURL;

@implementation XLHTTPRequest
#pragma mark - 用户管理(UserMgr)
/** 登录*/
+ (NSURLSessionTask *)UM_loginWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_Login parameters:parameters cache:nil success:success failure:failure];
}
/** 登出*/
+ (NSURLSessionTask *)UM_loginOutWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_LoginOut parameters:parameters cache:nil success:success failure:failure];
}
/** 得到短信验证码*/
+ (NSURLSessionTask *)UM_getSMSWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_GetSMS parameters:parameters cache:nil success:success failure:failure];
}
/** 检测短信验证码*/
+ (NSURLSessionTask *)UM_checkSMSWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_CheckSMS parameters:parameters cache:nil success:success failure:failure];
}
/** 重置密码*/
+ (NSURLSessionTask *)UM_resetPwdWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_ResetPwd parameters:parameters cache:nil success:success failure:failure];
}
/** 修改手机号码*/
+ (NSURLSessionTask *)UM_changePhoneNumberWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_ChangePhoneNumber parameters:parameters cache:nil success:success failure:failure];
}
/** 获取用户信息*/
+ (NSURLSessionTask *)UM_getUserInfoWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_GetUserInfo parameters:parameters cache:nil success:success failure:failure];
}
/** 修改用户名*/
+ (NSURLSessionTask *)UM_changeUserNameWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_ChangeUserName parameters:parameters cache:nil success:success failure:failure];
}
/** 修改所属医院*/
+ (NSURLSessionTask *)UM_changeHospitalNameWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_ChangeHospitalName parameters:parameters cache:nil success:success failure:failure];
}
/** 修改性别*/
+ (NSURLSessionTask *)UM_changeGenderWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_ChangeGender parameters:parameters cache:nil success:success failure:failure];
}
/** 上传头像*/
+ (NSURLSessionTask *)UM_uploadUserLogoWithUserID:(NSString *)userID token:(NSString *)token image:(UIImage *)image progress:(XLRequestProgress)progress success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    
    NSString *url = [NSString stringWithFormat:@"/api/UploadPic/UploadUserHeadPic?doctorID=%@&TokenCode=%@",userID,token];
    return [self uploadWithURL:url parameters:nil name:@"image" images:@[image] fileNames:nil imageScale:0.1 imageType:@"png" uploadProgress:progress success:success failure:failure];
}

/** 上传个人简介*/
+ (NSURLSessionTask *)UM_UploadProfileWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_UM_UploadProfile parameters:parameters cache:nil success:success failure:failure];
}

#pragma mark - 急救管理(EmergencyTreamentMgr)

/** 获取医生正在参与的急诊列表*/
+ (NSURLSessionTask *)ETM_getCurrentListWithParameters:(id)parameters responseCache:(XLRequestCache)responseCache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_GetCurrentList parameters:parameters cache:nil success:success failure:failure];
}

/** 将急诊置顶*/
+ (NSURLSessionTask *)ETM_setETToTopWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_SetETTop parameters:parameters cache:nil success:success failure:failure];
}

/** 取消急诊置顶*/
+ (NSURLSessionTask *)ETM_cancelETToTopWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_CancelETTop parameters:parameters cache:nil success:success failure:failure];
}

/** 结束会诊*/
+ (NSURLSessionTask *)ETM_overETWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_OverET parameters:parameters cache:nil success:success failure:failure];
}

/** 获取电子病历*/
+ (NSURLSessionTask *)ETM_getPatientRecordWithParameters:(id)parameters responseCache:(XLRequestCache)responseCache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_GetPatientRecord parameters:parameters cache:nil success:success failure:failure];
}

/** 获取病人基本信息*/
+ (NSURLSessionTask *)ETM_getPatientInfoWithParameters:(id)parameters responseCache:(XLRequestCache)responseCache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_GetPatientInfo parameters:parameters cache:nil success:success failure:failure];
}

/** 获取病史信息*/
+ (NSURLSessionTask *)ETM_getPatientHistoryWithParameters:(id)parameters responseCache:(XLRequestCache)responseCache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_GetPatientHistory parameters:parameters cache:nil success:success failure:failure];
}

/** 获取辅助检查信息*/
+ (NSURLSessionTask *)ETM_getCheckWithParameters:(id)parameters responseCache:(XLRequestCache)responseCache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_GetCheck parameters:parameters cache:nil success:success failure:failure];

}

/** 获取病情变化信息*/
+ (NSURLSessionTask *)ETM_getChangeWithParameters:(id)parameters responseCache:(XLRequestCache)responseCache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_GetChange parameters:parameters cache:nil success:success failure:failure];
}

/** 获取急诊实时数据*/
+ (NSURLSessionTask *)ETM_getRealTimeDataWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_GetRealTimeData parameters:parameters cache:nil success:success failure:failure];
}

/** 获取医生的历史急诊列表*/
+ (NSURLSessionTask *)ETM_getHistoryListWithParameters:(id)parameters responseCache:(XLRequestCache)responseCache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_ETM_GetHistoryList parameters:parameters cache:nil success:success failure:failure];
}
#pragma mark - 医生管理(doctor)
/** 医生退出会诊*/
+ (NSURLSessionTask *)Doctor_quitETWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_Doctor_QuitET parameters:parameters cache:nil success:success failure:failure];
}

/** 获取会诊中的医生列表*/
+ (NSURLSessionTask *)Doctor_getDoctorInETWithParameters:(id)parameters responseCache:(XLRequestCache)responseCache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_Doctor_GetDoctorInET parameters:parameters cache:nil success:success failure:failure];
}

/** 获取可加入到会诊中的医生列表*/
+ (NSURLSessionTask *)Doctor_getDoctorsCanJoinWithParameters:(id)parameters responseCache:(XLRequestCache)responseCache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_Doctor_GetDoctorCanJoin parameters:parameters cache:nil success:success failure:failure];
}

/** 添加医生到会诊*/
+ (NSURLSessionTask *)Doctor_addDoctorToETWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_Doctor_AddDoctorToET parameters:parameters cache:nil success:success failure:failure];
}

/** 获取融云Token*/
+ (NSURLSessionTask *)Doctor_getRYTokenWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_Doctor_GetRYToken parameters:parameters cache:nil success:success failure:failure];
}

/** 查询医生信息(通过容云ID)*/
+ (NSURLSessionTask *)Doctor_searchDoctorByRYIDWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    return [self requestWithURL:Web_Doctor_searchDoctorByRYID parameters:parameters cache:nil success:success failure:failure];
}

#pragma mark - 许可协议管理(AgreementInfo)
/** 获取许可信息*/
+ (NSURLSessionTask *)getAgreementInfoWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure{
    return [self requestWithURL:Web_GetAgreementInfo parameters:parameters cache:nil success:success failure:failure];
}
#pragma mark - 上传管理(UploadTemplate)
/** 上传检查服务信息*/
+ (NSURLSessionTask *)Upload_checkInfoWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure{
    return [self requestWithURL:Web_Upload_CheckInfo parameters:parameters cache:nil success:success failure:failure];
}
/**上传用药信息*/
+ (NSURLSessionTask *)Upload_medicineWithParameters:(id)parameters success:(XLRequestSuccess)success failure:(XLRequestFailure)failure{
    return [self requestWithURL:Web_Upload_Medicine parameters:parameters cache:nil success:success failure:failure];
}

/*
 配置好PPNetworkHelper各项请求参数,封装成一个公共方法,给以上方法调用,
 相比在项目中单个分散的使用PPNetworkHelper/其他网络框架请求,可大大降低耦合度,方便维护
 在项目的后期, 你可以在公共请求方法内任意更换其他的网络请求工具,切换成本小
 */
#pragma mark - 请求的公共方法
/** POST*/
+ (NSURLSessionTask *)requestWithURL:(NSString *)url parameters:(NSDictionary *)parameter cache:(XLRequestCache)cache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    // 在请求之前你可以统一配置你请求的相关参数 ,设置请求头, 请求参数的格式, 返回数据的格式....这样你就不需要每次请求都要设置一遍相关参数
    
    // 设置请求参数的类型:JSON,默认是HTTP
    [PPNetworkHelper setRequestSerializer:PPRequestSerializerJSON];
    
    //开启日志打印
    [PPNetworkHelper openLog];
    
    url = [UserInfo.serviceInfo.baseURL stringByAppendingString:url];
    
    //发起请求
    return [PPNetworkHelper POST:url parameters:parameter responseCache:cache != nil ? ^(id responseCache) {
//        XLLog(@"responseCache:%@",responseCache);
        //缓存
        cache(responseCache);
        
        
    }:nil  success:^(id responseObject) {
        // 在这里你可以根据项目自定义其他一些重复操作,比如加载页面时候的等待效果, 提醒弹窗....
        
        if ([responseObject[@"status"] isEqualToString:@"1"] || [responseObject[@"IsSuccessful"] integerValue] == 1) {
            
            if ([FunProgressHUD isVisible]) {
                [FunProgressHUD dismiss];
            }
            
            if(success){
                success(responseObject);
            }
        } else {
            if (failure) {
                NSString *info = [responseObject[@"info"] length] > 0 ? responseObject[@"info"] : responseObject[@"Msg"];
                
//                failure(nil,responseObject[@"info"]);
                failure(nil,info.length > 0 ? info : @"");
            }
        }
        
        
    } failure:^(NSError *error) {
        // 同上
        [FunProgressHUD showErrorWithStatus:NetworkTimeout];
        if (failure) {
           failure(error,nil);
        }
        
    }];
}

/** GET*/
+ (NSURLSessionTask *)getRequestWithURL:(NSString *)url parameters:(NSDictionary *)parameter cache:(XLRequestCache)cache success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    // 在请求之前你可以统一配置你请求的相关参数 ,设置请求头, 请求参数的格式, 返回数据的格式....这样你就不需要每次请求都要设置一遍相关参数
    
    // 设置请求参数的类型:JSON,默认是HTTP
    [PPNetworkHelper setRequestSerializer:PPRequestSerializerJSON];
    
    //开启日志打印
    [PPNetworkHelper openLog];
    
    url = [UserInfo.serviceInfo.baseURL stringByAppendingString:url];
    
    //发起请求
    return [PPNetworkHelper GET:url parameters:parameter responseCache:cache != nil ? ^(id responseCache) {
        //        XLLog(@"responseCache:%@",responseCache);
        //缓存
        cache(responseCache);
        
        
    }:nil success:^(id responseObject) {
        // 在这里你可以根据项目自定义其他一些重复操作,比如加载页面时候的等待效果, 提醒弹窗....
        if(success){
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        // 同上
        [FunProgressHUD showErrorWithStatus:NetworkTimeout];
        if (failure) {
            failure(error,nil);
        }
    }];
}

/** 上传图片*/
+ (NSURLSessionTask *)uploadWithURL:(NSString *)url parameters:(NSDictionary *)parameter name:(NSString *)name images:(NSArray<UIImage *>*)images fileNames:(NSArray<NSString *> *)fileNames imageScale:(CGFloat)imageScale imageType:(NSString *)imageType uploadProgress:(XLRequestProgress)uploadProgress success:(XLRequestSuccess)success failure:(XLRequestFailure)failure {
    // 在请求之前你可以统一配置你请求的相关参数 ,设置请求头, 请求参数的格式, 返回数据的格式....这样你就不需要每次请求都要设置一遍相关参数
    
    // 设置请求参数的类型:JSON,默认是HTTP
    [PPNetworkHelper setRequestSerializer:PPRequestSerializerJSON];
    
    //开启日志打印
    [PPNetworkHelper openLog];
    
    url = [UserInfo.serviceInfo.baseURL stringByAppendingString:url];
    
    //发起请求
    return [PPNetworkHelper uploadImagesWithURL:url parameters:parameter name:name images:images fileNames:fileNames imageScale:imageScale imageType:imageType progress:^(NSProgress *progress) {
        if (uploadProgress) {
            uploadProgress(progress);
        }
    } success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error,nil);
        }
    }];
}

@end
